package com.rzico.sbl.ui.report.depot

import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.utils.observe
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDepotInBinding
import com.rzico.sbl.model.DepotOutData
import com.rzico.sbl.viewmodel.ReportDepotViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class ReportDepotInActivity : BaseActivity(R.layout.activity_report_depot_in) {

    private val mBinding by lazy { ActivityReportDepotInBinding.bind(getRootView()) }

    private var mStatus = ""

    override fun getViewModel() = get<ReportDepotViewModel>()

    override fun initData() {
        initTitle(title = "入库")

        initLayout()
        initTabLayout()

        observe<String>("depotInList") { updateList() }
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关入库信息！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            if (!isLoadingMore) {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<DepotOutData>(R.layout.item_report_depot_in) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_depot_name, bean.name)
                                text(
                                    R.id.item_depot_time,
                                    when (mStatus) {
                                        "debit" -> "出库时间: ${bean.debitDate.getTimeString()}"
                                        else -> "入库时间: ${bean.creditDate.getTimeString()}"
                                    }
                                )

                                visibility(
                                    R.id.item_depot_bottom,
                                    visibleValue { mStatus == "debit" }
                                )

                                with<LinearLayout>(R.id.item_depot_sn) { layout ->
                                    layout.removeAllViews()

                                    bean.snList.forEach { item ->
                                        layout.addView(
                                            inflate<View>(R.layout.content_report_depot_station).apply {
                                                find<TextView>(R.id.conent_status).gone()
                                                find<TextView>(R.id.conent_title).text =
                                                    "${item.sn}(${item.name})"
                                            }
                                        )
                                    }
                                }

                                clicked(R.id.item_depot_btn) {
                                    startActivityEx<ReportDepotViewActivity>(
                                        "mode" to "check",
                                        "groupId" to bean.id
                                    )
                                }

                                clicked {
                                    if (mStatus == "credit")
                                        startActivityEx<ReportDepotViewActivity>(
                                            "groupId" to bean.id
                                        )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    /**
     * 初始化 TabLayout
     */
    private fun initTabLayout() {
        mBinding
            .depotTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.let {
                            mStatus = when (it.position) {
                                0 -> "debit"
                                1 -> "credit"
                                else -> ""
                            }

                            getViewModel().cancelRequest()
                            getViewModel().delay { updateList() }
                        }
                    }
                }

                addTab(this.newTab().setText("待入库"), true)
                addTab(this.newTab().setText("已入库"), false)
            }
    }

    /**
     * 库管合并入库列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .inList(
                page = index,
                status = mStatus,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}